package com.stylefeng.guns.modular.clm.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.aspectj.util.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.config.properties.GunsProperties;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.core.util.clm.AdminConstant;
import com.stylefeng.guns.core.util.clm.AdminTools;
import com.stylefeng.guns.core.util.clm.ClmAccessUser;
import com.stylefeng.guns.core.util.clm.RedisService;
import com.stylefeng.guns.modular.clm.service.IClmColumnRoomService;
import com.stylefeng.guns.modular.clm.service.IClmRoomCoursewareService;
import com.stylefeng.guns.modular.clm.service.IClmRoomService;
import com.stylefeng.guns.modular.clm.service.IColumnService;
import com.stylefeng.guns.modular.clm.service.ITeacherRoomService;
import com.stylefeng.guns.modular.clm.service.ITeacherService;
import com.stylefeng.guns.modular.system.model.ClmAreaEntity;
import com.stylefeng.guns.modular.system.model.ClmColumnRoom;
import com.stylefeng.guns.modular.system.model.ClmRoom;
import com.stylefeng.guns.modular.system.model.ClmRoomCourseware;
import com.stylefeng.guns.modular.system.model.ClmSchoolEntity;
import com.stylefeng.guns.modular.system.model.ClmSubjectEntity;
import com.stylefeng.guns.modular.system.model.Column;
import com.stylefeng.guns.modular.system.model.Teacher;
import com.stylefeng.guns.modular.system.model.TeacherRoom;
import com.stylefeng.guns.modular.system.service.IDictService;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-04-04 16:54:14
 */
@Controller
@RequestMapping("/clmRoom")
public class ClmRoomController extends BaseController {

    private final static Logger logger = LoggerFactory.getLogger(ClmRoomController.class);
	
    @Autowired
	private RedisService redisService ;
    
    private String PREFIX = "/clm/clmRoom/";

    @Autowired
    private IClmRoomService clmRoomService;
    
    @Autowired
    private GunsProperties gunsProperties;
    
    @Autowired
    private IClmColumnRoomService clmColumnRoomService;
    
    @Autowired
    private IClmRoomCoursewareService clmRoomCoursewareService;
    
    @Autowired
    private IDictService dictService;
    
    @Autowired
    private IColumnService columnService;
    
    @Autowired
    private ITeacherService teacherService;
    
    @Autowired
    private ITeacherRoomService teacherRoomService;
    
    

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index(Model model) {
    	try {
    		
    		/*List<com.dubbox.plat.entity.ClmAreaEntity> provinceList1 = PlatFactory.getClmAreaRemote().findProvinceList("002");
    		System.out.println(provinceList1.toString());*/
    		//获取省列表
    		List<ClmAreaEntity> l_province = null;
    		if(redisService.exists("clmAdminProvinceList"+AdminConstant.DEFAULT_SITEID)){
    			l_province = (List<ClmAreaEntity>) redisService.get("clmAdminProvinceList"+AdminConstant.DEFAULT_SITEID);
    			if(l_province !=null && l_province.size()>0){
    				model.addAttribute("provinceList",l_province);
    			}else{
    				String provinceList=AdminTools.findProvinceList("",AdminConstant.DEFAULT_SITEID);
    				if(StringUtils.isNotBlank(provinceList)){
    					JSONObject jsonObj=JSON.parseObject(provinceList);
    					l_province=JSON.parseArray(jsonObj.get("message").toString(), ClmAreaEntity.class);
    					model.addAttribute("provinceList",l_province);
    					redisService.set("clmAdminProvinceList"+AdminConstant.DEFAULT_SITEID, l_province);
    				}
    			}
    			
    		}else{
				String provinceList=AdminTools.findProvinceList("",AdminConstant.DEFAULT_SITEID);
				if(StringUtils.isNotBlank(provinceList)){
					JSONObject jsonObj=JSON.parseObject(provinceList);
					l_province=JSON.parseArray(jsonObj.get("message").toString(), ClmAreaEntity.class);
					model.addAttribute("provinceList",l_province);
					redisService.set("clmAdminProvinceList"+AdminConstant.DEFAULT_SITEID, l_province);
				}
			}
    		
			
			//获取科目列表
    		List<ClmSubjectEntity> l_subject = null;
    		if(redisService.exists("clmAdminSubjectList"+AdminConstant.DEFAULT_SITEID)){
    			l_subject = (List<ClmSubjectEntity>) redisService.get("clmAdminSubjectList"+AdminConstant.DEFAULT_SITEID);
    			if(l_subject !=null && l_subject.size()>0){
    				model.addAttribute("subjectList",l_subject);
    			}else{
    				String subjectList=AdminTools.findAllSubject(AdminConstant.DEFAULT_SITEID);
	    			if(StringUtils.isNotBlank(subjectList)){
	    				JSONObject jsonObj=JSON.parseObject(subjectList);
	    				l_subject=JSON.parseArray(jsonObj.get("allSubject").toString(), ClmSubjectEntity.class);
	    				model.addAttribute("subjectList",l_subject);
	    				redisService.set("clmAdminSubjectList"+AdminConstant.DEFAULT_SITEID, l_subject);
	    			}
    			}
    			
    		}else{
    			String subjectList=AdminTools.findAllSubject(AdminConstant.DEFAULT_SITEID);
    			if(StringUtils.isNotBlank(subjectList)){
    				JSONObject jsonObj=JSON.parseObject(subjectList);
    				l_subject=JSON.parseArray(jsonObj.get("allSubject").toString(), ClmSubjectEntity.class);
    				model.addAttribute("subjectList",l_subject);
    				redisService.set("clmAdminSubjectList"+AdminConstant.DEFAULT_SITEID, l_subject);
    			}
    		}
    		
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
        return PREFIX + "clmRoom.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/clmRoom_add")
    public String clmRoomAdd() {
        return PREFIX + "clmRoom_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/clmRoom_update/{clmRoomId}")
    public String clmRoomUpdate(@PathVariable String clmRoomId, Model model) {
        ClmRoom clmRoom = clmRoomService.selectById(clmRoomId);
        String imageUrl=clmRoom.getImageUrl();
        if(StringUtils.isNotBlank(imageUrl)){
        	imageUrl.substring(imageUrl.lastIndexOf("/"));
        	logger.info("imageUrl==="+imageUrl);
        	clmRoom.setImageUrl(imageUrl);
        }
        model.addAttribute("item",clmRoom);
        
        //名师列表
        Wrapper<Teacher> teacherWrapper=null ;
        EntityWrapper<Teacher> teacherEntityWrapper=new EntityWrapper<Teacher>();
        teacherEntityWrapper.eq("site_id", clmRoom.getSiteId());
        teacherWrapper=teacherEntityWrapper.eq("be_delete", "0");
        List<Teacher> l_teacher=teacherService.selectList(teacherWrapper);
        model.addAttribute("l_teacher",l_teacher);
        
        //查询课堂名师
        Wrapper<TeacherRoom> teacherRoomWrapper=null ;
        EntityWrapper<TeacherRoom> teacherRoomEntityWrapper=new EntityWrapper<TeacherRoom>();
        teacherRoomEntityWrapper.eq("room_id", clmRoom.getRoomId());
        teacherRoomWrapper=teacherRoomEntityWrapper.eq("be_delete", "0");
        TeacherRoom teacherRoom =teacherRoomService.selectOne(teacherRoomWrapper);
        if(teacherRoom!=null){
        	model.addAttribute("starTeacherId",teacherRoom.getTeacherId());
        }
        
        //查询课堂关联的栏目专题
        EntityWrapper<Column> columnEntityWrapper=new EntityWrapper<Column>();
    	Wrapper<Column> columnWrapper=null ;
    	columnEntityWrapper.eq("site_id", clmRoom.getSiteId());
    	columnEntityWrapper.eq("be_delete", "0");
    	columnEntityWrapper.eq("status", "1");
    	columnWrapper=columnEntityWrapper.orderBy("num", true);
        List<Column> l_column = columnService.selectList(columnWrapper);
        
        //查询课程专题关联表
        Map<String,String> m=new HashMap<String,String>();
        Wrapper<ClmColumnRoom> wrapper=null ;
        EntityWrapper<ClmColumnRoom> entityWrapper=new EntityWrapper<ClmColumnRoom>();
        wrapper=entityWrapper.eq("room_id", clmRoom.getRoomId());
        wrapper=entityWrapper.eq("site_id", clmRoom.getSiteId());
        wrapper=entityWrapper.eq("is_delete","0");
        List<ClmColumnRoom> l_clmColumnRoom=clmColumnRoomService.selectList(wrapper);
        if(l_clmColumnRoom!=null && l_clmColumnRoom.size()>0){
        	for(ClmColumnRoom clmColumnRoom : l_clmColumnRoom){
        		m.put(clmColumnRoom.getColumnId(), clmColumnRoom.getColumnId());
        	}
        }
        
        //判断专题栏目是否被选中
        StringBuffer ids= new StringBuffer();
        if(l_column!=null && l_column.size()>0){
        	for(Column cloumn : l_column){
        		if(AdminConstant.COLUMN_MS.equals(cloumn.getColumnDesc())){
        			//名师栏目
        			if(teacherRoom!=null){
        				cloumn.setBeEdit("0");
        			}else{
        				cloumn.setBeEdit("1");
        			}
        		}else{
        			if(m.containsKey(cloumn.getColumnId()+"")){
            			cloumn.setBeEdit("0");//此处做是否关联删除标识用 0-未删除1-删除
            		}else{
            			cloumn.setBeEdit("1");
            		}
            		
        		}
        		ids.append(cloumn.getColumnId()).append(",");
        	}
        }
        String column_ids="";
        if(ids.length()>0){
        	column_ids=ids.substring(0, ids.lastIndexOf(","));
        }
        model.addAttribute("column_ids",column_ids);
        model.addAttribute("l_column",l_column);
        
        //查询课堂录制件
        Wrapper<ClmRoomCourseware> roomCoursewareWrapper=null ;
        EntityWrapper<ClmRoomCourseware> coursewareWrapper=new EntityWrapper<ClmRoomCourseware>();
        coursewareWrapper.eq("room_id", clmRoom.getRoomId());
        roomCoursewareWrapper=coursewareWrapper.eq("site_id", clmRoom.getSiteId());
        List<ClmRoomCourseware> l_roomCourseware=clmRoomCoursewareService.selectList(roomCoursewareWrapper);
        model.addAttribute("l_roomCourseware",l_roomCourseware);
        
        
        
        LogObjectHolder.me().set(clmRoom);
        return PREFIX + "clmRoom_edit.html";
    }
    
    /**
     * 跳转到公用修改
     */
    @RequestMapping("/clmRoom_utilUpdate/{clmRoomId}")
    public String clmRoomUtilUpdate(@PathVariable String clmRoomId, Model model) {
        ClmRoom clmRoom = clmRoomService.selectById(clmRoomId);
        String imageUrl=clmRoom.getImageUrl();
        if(StringUtils.isNotBlank(imageUrl)){
        	imageUrl.substring(imageUrl.lastIndexOf("/"));
        	logger.info("imageUrl==="+imageUrl);
        	clmRoom.setImageUrl(imageUrl);
        }
        model.addAttribute("item",clmRoom);
        
        //名师列表
        Wrapper<Teacher> teacherWrapper=null ;
        EntityWrapper<Teacher> teacherEntityWrapper=new EntityWrapper<Teacher>();
        teacherEntityWrapper.eq("site_id", clmRoom.getSiteId());
        teacherWrapper=teacherEntityWrapper.eq("be_delete", "0");
        List<Teacher> l_teacher=teacherService.selectList(teacherWrapper);
        model.addAttribute("l_teacher",l_teacher);
        
        //查询课堂名师
        Wrapper<TeacherRoom> teacherRoomWrapper=null ;
        EntityWrapper<TeacherRoom> teacherRoomEntityWrapper=new EntityWrapper<TeacherRoom>();
        teacherRoomEntityWrapper.eq("room_id", clmRoom.getRoomId());
        teacherRoomWrapper=teacherRoomEntityWrapper.eq("be_delete", "0");
        TeacherRoom teacherRoom =teacherRoomService.selectOne(teacherRoomWrapper);
        if(teacherRoom!=null){
        	model.addAttribute("starTeacherId",teacherRoom.getTeacherId());
        }
        
        //查询课堂关联的栏目专题
        EntityWrapper<Column> columnEntityWrapper=new EntityWrapper<Column>();
    	Wrapper<Column> columnWrapper=null ;
    	columnEntityWrapper.eq("site_id", clmRoom.getSiteId());
    	columnEntityWrapper.eq("be_delete", "0");
    	columnEntityWrapper.eq("status", "1");
    	columnWrapper=columnEntityWrapper.orderBy("num", true);
        List<Column> l_column = columnService.selectList(columnWrapper);
        
        //查询课程专题关联表
        Map<String,String> m=new HashMap<String,String>();
        Wrapper<ClmColumnRoom> wrapper=null ;
        EntityWrapper<ClmColumnRoom> entityWrapper=new EntityWrapper<ClmColumnRoom>();
        wrapper=entityWrapper.eq("room_id", clmRoom.getRoomId());
        wrapper=entityWrapper.eq("site_id", clmRoom.getSiteId());
        wrapper=entityWrapper.eq("is_delete","0");
        List<ClmColumnRoom> l_clmColumnRoom=clmColumnRoomService.selectList(wrapper);
        if(l_clmColumnRoom!=null && l_clmColumnRoom.size()>0){
        	for(ClmColumnRoom clmColumnRoom : l_clmColumnRoom){
        		m.put(clmColumnRoom.getColumnId(), clmColumnRoom.getColumnId());
        	}
        }
        
        //判断专题栏目是否被选中
        StringBuffer ids= new StringBuffer();
        if(l_column!=null && l_column.size()>0){
        	for(Column cloumn : l_column){
        		if(AdminConstant.COLUMN_MS.equals(cloumn.getColumnDesc())){
        			//名师栏目
        			if(teacherRoom!=null){
        				cloumn.setBeEdit("0");
        			}else{
        				cloumn.setBeEdit("1");
        			}
        		}else{
        			if(m.containsKey(cloumn.getColumnId()+"")){
            			cloumn.setBeEdit("0");//此处做是否关联删除标识用 0-未删除1-删除
            		}else{
            			cloumn.setBeEdit("1");
            		}
            		
        		}
        		ids.append(cloumn.getColumnId()).append(",");
        	}
        }
        String column_ids="";
        if(ids.length()>0){
        	column_ids=ids.substring(0, ids.lastIndexOf(","));
        }
        model.addAttribute("column_ids",column_ids);
        model.addAttribute("l_column",l_column);
        
        //查询课堂录制件
        Wrapper<ClmRoomCourseware> roomCoursewareWrapper=null ;
        EntityWrapper<ClmRoomCourseware> coursewareWrapper=new EntityWrapper<ClmRoomCourseware>();
        coursewareWrapper.eq("room_id", clmRoom.getRoomId());
        roomCoursewareWrapper=coursewareWrapper.eq("site_id", clmRoom.getSiteId());
        List<ClmRoomCourseware> l_roomCourseware=clmRoomCoursewareService.selectList(roomCoursewareWrapper);
        model.addAttribute("l_roomCourseware",l_roomCourseware);
        
        LogObjectHolder.me().set(clmRoom);
        return PREFIX + "clmRoom_utilEdit.html";
    }

    
    
    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(ClmRoom clmRoom) {
    	EntityWrapper<ClmRoom> entityWrapper=new EntityWrapper<ClmRoom>();
    	Wrapper<ClmRoom> wrapper=null ;
    	logger.info("==================="+clmRoom.toString());
    	if(StringUtils.isNotBlank(clmRoom.getTeacherName())){
    		entityWrapper.eq("teacher_name", clmRoom.getTeacherName());
    	}
    	if(StringUtils.isNotBlank(clmRoom.getOrgProvinceCode())){
    		entityWrapper.eq("org_province_code", clmRoom.getOrgProvinceCode());
    	}
    	if(StringUtils.isNotBlank(clmRoom.getOrgCityCode())){
    		entityWrapper.eq("org_city_code", clmRoom.getOrgCityCode());
    	}
    	if(StringUtils.isNotBlank(clmRoom.getOrgAreaCode())){
    		entityWrapper.eq("org_area_code", clmRoom.getOrgAreaCode());
    	}
    	if(StringUtils.isNotBlank(clmRoom.getSchoolId())){
    		entityWrapper.eq("school_id", clmRoom.getSchoolId());
    	}
    	/*if(StringUtils.isNotBlank(clmRoom.getRoomId())){
    		entityWrapper.eq("room_id", clmRoom.getRoomId());
    	}*/
    	if(StringUtils.isNotBlank(clmRoom.getNumber())){
    		entityWrapper.eq("number_", clmRoom.getNumber());
    	}
    	if(StringUtils.isNotBlank(clmRoom.getSubject())){
    		entityWrapper.like("subject_", clmRoom.getSubject());
    	}
    	if(StringUtils.isNotBlank(clmRoom.getLiveType())){
    		entityWrapper.eq("live_type", clmRoom.getLiveType());
    	}
    	if(StringUtils.isNotBlank(clmRoom.getPublicScope())){
    		entityWrapper.eq("public_scope", clmRoom.getPublicScope());
    	}
    	if(StringUtils.isNotBlank(clmRoom.getCourseCode())){
    		entityWrapper.eq("course_code", clmRoom.getCourseCode());
    	}
    	if(ToolUtil.isNotEmpty(this.getPara("startDate1"))){
    		entityWrapper.ge("start_date", this.getPara("startDate1"));
    	}
    	if(ToolUtil.isNotEmpty(this.getPara("endDate1"))){
    		entityWrapper.le("invalid_date", this.getPara("endDate1"));
    	}
    	if(StringUtils.isNotBlank(this.getPara("roomStatus"))){
    		Date day=new Date();    
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    		if("1".equals(this.getPara("roomStatus"))){
    			//正在直播即将开始
    			entityWrapper.ge("invalid_date", df.format(day));
    		}else if("2".equals(this.getPara("roomStatus"))){
    			//已经结束
    			entityWrapper.le("invalid_date", df.format(day));
    		}
    	}   
    	
    	entityWrapper.eq("site_id", AdminConstant.DEFAULT_SITEID);
    	entityWrapper.eq("be_deleted", 0);
    	wrapper=entityWrapper.orderBy("start_date", false);
    	List<ClmRoom> l_clmRoom = clmRoomService.selectList(wrapper);
    	
    	return l_clmRoom;
    }

    /**
     * 新增
     */
   /* @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(ClmRoom clmRoom) {
        clmRoomService.insert(clmRoom);
        return SUCCESS_TIP;
    }*/

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam String roomId) {
       // clmRoomService.deleteById(clmRoomId);
        ClmRoom  clmRoom = clmRoomService.selectById(roomId);
        clmRoom.setBeDeleted(1);
        clmRoomService.updateById(clmRoom);
        redisService.remove(AdminConstant.ROOM_INDEX_LIST+AdminConstant.DEFAULT_SITEID);
        //删除栏目中关联的课程
        
    	Map<String, Object> columnMap=new HashMap<String, Object>();
    	columnMap.put("room_id", roomId);
    	columnMap.put("site_id", AdminConstant.DEFAULT_SITEID);
    	clmColumnRoomService.deleteByMap(columnMap);
    	
        //删除名师中关联的课程
    	Map<String, Object> teacherRoomMap=new HashMap<String, Object>();
    	teacherRoomMap.put("room_id", roomId);
    	//teacherRoomMap.put("site_id", AdminConstant.DEFAULT_SITEID);
    	teacherRoomService.deleteByMap(teacherRoomMap);
        
        return SUCCESS_TIP;
    }

    /**
     * 修改
     * @throws IOException 
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(ClmRoom clmRoom) {
    	logger.info(clmRoom.toString());
    	String picture=this.getPara("avatar");
    	String uploadUrl = "_tmp/" + picture;
    	String realPath ="";
    	
    	try {
    		ClmRoom ClmRoom_old=clmRoomService.selectById(clmRoom.getRoomId());
    		String userId=ClmRoom_old.getTeacherId();
    		String imageUrl=ClmRoom_old.getImageUrl();
    		if(StringUtils.isBlank(picture) || (StringUtils.isNotBlank(imageUrl) && imageUrl.contains(picture))){
    			//没有修改图片 不做处理
    		}else{
    			//有上传图片
    			if(StringUtils.isNotBlank(picture)){
        			//在本地保存上传的图片
        			String adminUploadUrl = gunsProperties.getFileUploadPath() + picture;
            		File fromFile=new File(adminUploadUrl);
                	File toFile=new File(gunsProperties.getFileAdminUploadPath()+picture);
                	FileUtil.copyFile(fromFile, toFile);
                	
                	//掉接口修改课程图片
        			String responJson=AdminTools.updateImageUrl(userId, uploadUrl,ClmRoom_old.getSiteId());
        			logger.info("update img="+responJson);
        			if(StringUtils.isNotBlank(responJson)){
        				JSONObject jsonObj=JSON.parseObject(responJson);
        				//{"result":0,"message":"图片修改失败"}
        				
        				if(!"0".equals(jsonObj.getString("result"))){
        					realPath=jsonObj.getString("realPath");
        					ClmRoom_old.setImageUrl(realPath);
        				}
        				
        			}
        			
        		}
    		}
    		
    		
    		//修改课堂基本信息
    		ClmRoom_old.setTeacherName(clmRoom.getTeacherName());
    		ClmRoom_old.setSchoolName(clmRoom.getSchoolName());
    		ClmRoom_old.setSubject(clmRoom.getSubject());
    		ClmRoom_old.setDescription(clmRoom.getDescription());
			clmRoomService.updateById(ClmRoom_old);
			
			//修改课程对应栏目
			
			String column_ids=this.getPara("column_ids");
			String[] sids=column_ids.split(",");
			if(StringUtils.isNotBlank(sids[0])){
				for(int i=0;i<sids.length;i++){
					String a=this.getPara("column"+sids[i]);
					String b=this.getPara("columnOld"+sids[i]);
					if(!this.getPara("column"+sids[i]).equals(this.getPara("columnOld"+sids[i]))){
						updateClmColumnRoom(ClmRoom_old.getRoomId(),ClmRoom_old.getSiteId(),sids[i],this.getPara("column"+sids[i]));
					}
				}
			}
			
			String isSelectedMS=this.getPara("isSelectedMS");//是否选择了名师栏目 0-是 1-否
			//添加名师课程
			String starTeacherId=this.getPara("starTeacherId");
			String starTeacherId_old=this.getPara("starTeacherId_old");
			TeacherRoom teacherRoom=new TeacherRoom();
			teacherRoom.setRoomId(clmRoom.getRoomId());
			
			Wrapper<TeacherRoom> teacherRoomWrapper=null ;
	        EntityWrapper<TeacherRoom> teacherRoomEntityWrapper=new EntityWrapper<TeacherRoom>();
	        
			if("0".equals(isSelectedMS)){
				//添加修改名师
				if(StringUtils.isNotBlank(starTeacherId_old)){
					//修改名师
					teacherRoomEntityWrapper.eq("room_id", clmRoom.getRoomId());
					teacherRoomEntityWrapper.eq("teacher_id", starTeacherId_old);
					teacherRoomWrapper=teacherRoomEntityWrapper.eq("be_delete", "0");
					teacherRoom=teacherRoomService.selectOne(teacherRoomWrapper);
					if(teacherRoom!=null){
						teacherRoom.setTeacherId(starTeacherId);
						teacherRoomService.updateById(teacherRoom);
					}else{
						teacherRoom.setTeacherId(starTeacherId);
						teacherRoom.setBeDelete("0");
						teacherRoomService.insert(teacherRoom);
					}
				}else{
					//添加名师
					teacherRoom.setTeacherId(starTeacherId);
					teacherRoom.setBeDelete("0");
					teacherRoomService.insert(teacherRoom);
				}
			}else{
				//删除名师
				if(StringUtils.isNotBlank(starTeacherId_old)){
					teacherRoomEntityWrapper.eq("room_id", clmRoom.getRoomId());
					teacherRoomEntityWrapper.eq("teacher_id", starTeacherId_old);
					teacherRoomWrapper=teacherRoomEntityWrapper.eq("be_delete", "0");
					teacherRoom=teacherRoomService.selectOne(teacherRoomWrapper);
					if(teacherRoom!=null){
						teacherRoomService.deleteById(teacherRoom.getId());
					}
				}
			}
			
			
			
			//修改课程录制件播放
			String be_deleted="0"; //是否删除 0-否 1-是
			if(StringUtils.isNotBlank(this.getPara("courseware_selected"))){
				String[] courseware_selected=this.getPara("courseware_selected").split(",");
				logger.info("courseware_selected="+courseware_selected.toString());
				if(courseware_selected.length>0){
					be_deleted="0";
					for(int i=0;i<courseware_selected.length;i++){
						clmRoomCoursewareService.updateRoomCoursewareByRoomIdAndCoursewareId(clmRoom.getRoomId(), courseware_selected[i], clmRoom.getSiteId(),be_deleted);
					}
				}
			}
			
			if(StringUtils.isNotBlank(this.getPara("courseware_unselected"))){
				String[] courseware_unselected=this.getPara("courseware_unselected").split(",");
				logger.info("courseware_unselected="+courseware_unselected.toString());
				if(courseware_unselected.length>0){
					be_deleted="1";
					if(courseware_unselected.length>0){
						for(int i=0;i<courseware_unselected.length;i++){
							clmRoomCoursewareService.updateRoomCoursewareByRoomIdAndCoursewareId(clmRoom.getRoomId(), courseware_unselected[i], clmRoom.getSiteId(),be_deleted);
						}
					}
				}
			}
			
			redisService.remove(AdminConstant.ROOM_INDEX_LIST+AdminConstant.DEFAULT_SITEID);
	        return SUCCESS_TIP;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ERROR;
		}
    }

    /**
     * 修改专题表
     * @param room_id
     * @param site_id
     * @param column_id
     * @param column_value
     * @return
     */
    private boolean updateClmColumnRoom(String room_id,String site_id,String column_id,String column_value){
    	boolean flag=false;
    	Wrapper<ClmColumnRoom> wrapper=null ;
        EntityWrapper<ClmColumnRoom> entityWrapper=new EntityWrapper<ClmColumnRoom>();
        wrapper = entityWrapper.eq("room_id", room_id).eq("site_id", site_id).eq("column_id", column_id);
        ClmColumnRoom clmColumnRoom=clmColumnRoomService.selectOne(wrapper);
        if(clmColumnRoom!=null){
        	clmColumnRoom.setIsDelete(column_value);
        	flag=clmColumnRoomService.updateById(clmColumnRoom);
        }else{
        	clmColumnRoom=new ClmColumnRoom();
        	String uuid = UUID.randomUUID().toString(); //转化为String对象
        	clmColumnRoom.setRoomId(room_id);
        	clmColumnRoom.setSiteId(site_id);
        	clmColumnRoom.setColumnId(column_id);
        	flag=clmColumnRoomService.insert(clmColumnRoom);
        }
        
        return flag;
        
    }
    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{roomId}")
    @ResponseBody
    public Object detail(@PathVariable("roomId") Integer clmRoomId) {
        return clmRoomService.selectById(clmRoomId);
    }
    
    /**
     * 查看课程视频
     * @return
     */
    @RequestMapping(value = "/lookRoom")
    @ResponseBody
    public String lookRoom() {
    	try {
    		String roomId=this.getPara("roomId");
    		String siteId=this.getPara("siteId");
    		//String userId=ClmAccessUser.getSSOUserId();
    		String userId="";
			String lookUrl=AdminTools.getLookUrlByRoomId(roomId,siteId,userId);
			JSONObject jsonObj=JSON.parseObject(lookUrl);
			if(StringUtils.isNotBlank(lookUrl)){
				jsonObj=JSON.parseObject(lookUrl);
				if(jsonObj.get("lookUrl")!=null){
					String url=AdminTools.SiteIdMap.get(siteId)+"/";
					jsonObj.put("lookUrl", url+jsonObj.get("lookUrl").toString());
				}
			}
			return jsonObj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return null;
    	
    }
    
    /**
     * 获取省下的市
     */
    @RequestMapping(value = "/getCityList")
    @ResponseBody
    public Object getCityList() {
    	//获取省列表
    	String provinceCode=this.getPara("provinceCode");
    	List<ClmAreaEntity> l_city = null;
		try {
			String cityList = AdminTools.findCityList(provinceCode,AdminConstant.DEFAULT_SITEID);
			if(StringUtils.isNotBlank(cityList)){
				JSONObject jsonObj=JSON.parseObject(cityList);
				l_city=JSON.parseArray(jsonObj.get("message").toString(), ClmAreaEntity.class);
				
			}
		} catch (Exception e) {
			logger.info("获取市失败！");
			e.printStackTrace();
		}
		return l_city;
    }
    
    /**
     * 获取市下的区县
     */
    @RequestMapping(value = "/getDiscList")
    @ResponseBody
    public Object getDiscList() {
    	//获取省列表
    	String cityCode=this.getPara("cityCode");
    	List<ClmAreaEntity> l_disc = null;
		try {
			String discList = AdminTools.findDiscList(cityCode,AdminConstant.DEFAULT_SITEID);
			if(StringUtils.isNotBlank(discList)){
				JSONObject jsonObj=JSON.parseObject(discList);
				l_disc=JSON.parseArray(jsonObj.get("message").toString(), ClmAreaEntity.class);
				
			}
		} catch (Exception e) {
			logger.info("获取市失败！");
			e.printStackTrace();
		}
		return l_disc;
    }
    
    /**
     * 获取区县下学校
     */
    @RequestMapping(value = "/getSchoolList")
    @ResponseBody
    public Object getSchoolList() {
    	//获取学校列表
    	String code=this.getPara("code");//地区code
    	String type=this.getPara("type"); //0-省 1-市 2-区县
    	List<ClmSchoolEntity> l_school = null;
		try {
			String schoolList = AdminTools.findSchoolList(code, type,AdminConstant.DEFAULT_SITEID);
			if(StringUtils.isNotBlank(schoolList)){
				JSONObject jsonObj=JSON.parseObject(schoolList);
				l_school=JSON.parseArray(jsonObj.get("message").toString(), ClmSchoolEntity.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return l_school;
    }
    
 /*   @RequestMapping("/redisTest")
	public String redisTest(){
		String tmp="";
		if(redisService.exists("aaa")){
			logger.info("111===="+redisService.get("aaa").toString());
		}else{
			redisService.set("aaa","value2就看见22");
			logger.info("2222===="+redisService.get("aaa").toString());
			return tmp;
		}
		//tmp=redisService.get("aaa").toString();
		//redisService.remove("aaa");
		
		return tmp;
	}*/

}
